package com.sesame.mq.rabbit.topic.controller;

import com.alibaba.fastjson.JSON;
import com.sesame.bean.User;
import com.sesame.mq.rabbit.topic.config.RabbitTopicConfig;
import kim.sesame.common.utils.UUIDUtil;
import kim.sesame.common.web.controller.AbstractWebController;
import kim.sesame.common.result.ApiResult;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CommonsLog
@RestController
@RequestMapping("/topic")
public class TopicSendController extends AbstractWebController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * http://127.0.0.1:2020/rabbitmq/topic/send?name=aaa&key=topic.user
     * http://127.0.0.1:2020/rabbitmq/topic/send?name=aaa&key=aaa.bbb
     */
    @RequestMapping("/send")
    public ApiResult directSend(String name, String key) {
        if (StringUtils.isEmpty(key)) {
            key = "topic.user";
        }
        User user = new User(UUIDUtil.getShortUUID(), name);

        String json = JSON.toJSONString(user);
        amqpTemplate.convertAndSend(RabbitTopicConfig.exchange, key, json);

        return success();
    }

}
